package com.tpshion.cloud.base.util;

import com.tpshion.cloud.common.support.Assert;
import com.tpshion.cloud.common.support.ErrorCode;
import org.springframework.util.CollectionUtils;
import java.util.List;

public class ListUtil {

    public static <T> List<T> getPage(List<T> list, Integer pageNum, Integer pageSize){
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        if (pageNum == 0){
            pageNum = 1;
        }
        int toIndex = pageNum * pageSize;
        int fromIndex = (pageNum - 1) * pageSize;
        Assert.isTrue(fromIndex < list.size(), ErrorCode.FAIL.getCode(),"页数超出最大值");
        if(toIndex > list.size()){
            toIndex = list.size();
        }
        return list.subList(fromIndex,toIndex);
    }
}
